package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.service.OnlineEditorService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.paas.toolbox.wrap.BaseVO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.onlineEditor.dto.ContractTemplateBaseDeleteDTO;
import com.xbongbong.pro.onlineEditor.dto.ContractTemplateBaseEnableDTO;
import com.xbongbong.pro.onlineEditor.dto.ContractTemplateBaseListDTO;
import com.xbongbong.pro.onlineEditor.dto.ContractTemplateBaseQueryDTO;
import com.xbongbong.pro.onlineEditor.dto.ContractTemplateBaseSaveDTO;
import com.xbongbong.pro.onlineEditor.dto.FileCopySaveDTO;
import com.xbongbong.pro.onlineEditor.dto.FileDetailDTO;
import com.xbongbong.pro.onlineEditor.dto.FileRenameDTO;
import com.xbongbong.pro.onlineEditor.sdk.OnlineEditorResultVO;
import com.xbongbong.pro.onlineEditor.sdk.dto.FiledUploadDTO;
import com.xbongbong.pro.onlineEditor.sdk.vo.FileWatermarkVO;
import com.xbongbong.pro.onlineEditor.sdk.vo.FiledUploadVO;
import com.xbongbong.pro.onlineEditor.sdk.vo.GetFileDownloadUrlVO;
import com.xbongbong.pro.onlineEditor.sdk.vo.GetFileInfoVO;
import com.xbongbong.pro.onlineEditor.sdk.vo.GetFilePermissionVO;
import com.xbongbong.pro.onlineEditor.sdk.vo.ListUserInfoVO;
import com.xbongbong.pro.onlineEditor.vo.ContractTemplateBaseListVO;
import com.xbongbong.pro.onlineEditor.vo.ContractTemplateBaseQueryVO;
import com.xbongbong.pro.onlineEditor.vo.FileCopySaveVO;
import com.xbongbong.pro.onlineEditor.vo.FileDetailVO;
import lombok.AllArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author: yingjf
 * @date: 2023/3/8 13:51
 * @description: 在线编辑器
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/onlineEditor")
@AllArgsConstructor
public class OnlineEditorController {

    private final OnlineEditorService onlineEditorService;

    /**
     * 获取文件信息
     * date: 2023/3/6 19:19
     *
     * @param fileId
     * @author yingjf
     */
    @RequestMapping(value = "/v3/3rd/files/{fileId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getFileInfo(@PathVariable("fileId") String fileId) {
        OnlineEditorResultVO<GetFileInfoVO> response;
        GetFileInfoVO getFileInfoVo = onlineEditorService.getFileInfo(fileId);
        response = new OnlineEditorResultVO<>(getFileInfoVo);
        return JSON.toJSONString(response);
    }

    /**
     * 获取文件下载地址
     * date: 2023/3/6 19:19
     *
     * @param fileId
     * @author yingjf
     */
    @RequestMapping(value = "/v3/3rd/files/{fileId}/download", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getFileDownloadUrl(@PathVariable("fileId") String fileId) {
        OnlineEditorResultVO<GetFileDownloadUrlVO> response;
        GetFileDownloadUrlVO fileDownloadUrl = onlineEditorService.getFileDownloadUrl(fileId);
        response = new OnlineEditorResultVO<>(fileDownloadUrl);
        return JSON.toJSONString(response);
    }

    /**
     * 获取文档用户权限
     * date: 2023/3/6 19:21
     *
     * @param fileId
     * @author yingjf
     */
    @RequestMapping(value = "/v3/3rd/files/{fileId}/permission", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getFilePermission(@PathVariable("fileId") String fileId) {
        OnlineEditorResultVO<GetFilePermissionVO> response;
        GetFilePermissionVO getFilePermissionVO = new GetFilePermissionVO("1", 1, 1, 1, 0, 0, 0, 0, 1, 1);
        response = new OnlineEditorResultVO<>(getFilePermissionVO);
        return JSON.toJSONString(response);
    }

    /**
     * 单阶段提交
     * date: 2023/3/6 19:36
     *
     * @param fileId
     * @param filedUploadDTO
     * @author yingjf
     * @see /pro/v1/file/excel/import/form/data
     */
    @RequestMapping(value = "/v3/3rd/files/{fileId}/upload")
    public String filedUpload(@PathVariable("fileId") String fileId, FiledUploadDTO filedUploadDTO, @RequestHeader HttpHeaders headers) {
        OnlineEditorResultVO<FiledUploadVO> response;
//        FiledUploadVO filedUploadVO = new FiledUploadVO("id" + DateTimeUtil.getInt(), "name.doc", 1, 1024, 100000L, 200000L, "1", "1");
        FiledUploadVO filedUploadVO = onlineEditorService.filedUpload(fileId, filedUploadDTO);
        response = new OnlineEditorResultVO<>(filedUploadVO);
        return JSON.toJSONString(response);
    }

    /**
     * 批量获取用户信息
     * date: 2023/3/6 20:00
     *
     * @param
     * @author yingjf
     */
    @RequestMapping(value = "/v3/3rd/users", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String listUserInfo(@RequestParam(value="user_ids",required = false) List<String> userIds) {
        OnlineEditorResultVO<List<ListUserInfoVO>> response;
        response = new OnlineEditorResultVO<>(onlineEditorService.listUserInfo( userIds));
        return JSON.toJSONString(response);
    }


    /**
     * 文档水印
     * date: 2023/3/6 20:05
     *
     * @param fileId
     * @author yingjf
     */
    @RequestMapping(value = "/v3/3rd/files/{fileId}/watermark", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String fileWatermark(@PathVariable("fileId") String fileId) {
        OnlineEditorResultVO<FileWatermarkVO> response;
        FileWatermarkVO fileWatermarkVO = new FileWatermarkVO(1, "我是水印", "rgba( 192, 192, 192, 0.6 )", "bold 20px Serif", -0.7853982f, 1, 1);
        response = new OnlineEditorResultVO<>(fileWatermarkVO);
        return JSON.toJSONString(response);
    }

    /**
     * 合同模板库-列表
     * date: 2023/3/8 14:51
     *
     * @param contractTemplateBaseListDTO
     * @param br
     * @author yingjf
     */
    @RequestMapping(value = "/contractTemplateBase/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String contractTemplateBaseList(@RequestBody @Valid ContractTemplateBaseListDTO contractTemplateBaseListDTO, BindingResult br) {
        ContractTemplateBaseListVO contractTemplateBaseListVO;
        XbbResponse<ContractTemplateBaseListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                contractTemplateBaseListVO = onlineEditorService.contractTemplateBaseList(contractTemplateBaseListDTO);
                response = new XbbResponse<>(contractTemplateBaseListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 合同模板库-启用
     * date: 2023/3/8 14:51
     *
     * @param contractTemplateBaseEnableDTO
     * @param br
     * @author yingjf
     */
    @RequestMapping(value = "/contractTemplateBase/enable", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String contractTemplateBaseEnable(@RequestBody @Valid ContractTemplateBaseEnableDTO contractTemplateBaseEnableDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                onlineEditorService.contractTemplateBaseEnable(contractTemplateBaseEnableDTO);
                response = new XbbResponse<>(new BaseVO());
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 合同模板库-删除
     * date: 2023/3/8 15:03
     *
     * @param contractTemplateBaseDeleteDTO
     * @param br
     * @author yingjf
     */
    @RequestMapping(value = "/contractTemplateBase/delete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String contractTemplateBaseDelete(@RequestBody @Valid ContractTemplateBaseDeleteDTO contractTemplateBaseDeleteDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                onlineEditorService.contractTemplateBaseDelete(contractTemplateBaseDeleteDTO);
                response = new XbbResponse<>(new BaseVO());
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 合同模板库-保存或新增
     * date: 2023/3/8 16:04
     *
     * @param contractTemplateBaseSaveDTO
     * @param br
     * @author yingjf
     */
    @RequestMapping(value = "/contractTemplateBase/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String contractTemplateBaseSave(@RequestBody @Valid ContractTemplateBaseSaveDTO contractTemplateBaseSaveDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                onlineEditorService.contractTemplateBaseSave(contractTemplateBaseSaveDTO);
                response = new XbbResponse<>(new BaseVO());
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 列表页查询
     * date: 2023/3/10 10:19
     * @author yingjf
     * @param contractTemplateBaseQueryDTO
     * @param br
     */
    @RequestMapping(value = "/contractTemplateBase/qurey", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String contractTemplateBaseQurey(@RequestBody @Valid ContractTemplateBaseQueryDTO contractTemplateBaseQueryDTO, BindingResult br) {
        XbbResponse<ContractTemplateBaseQueryVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ContractTemplateBaseQueryVO contractTemplateBaseQueryVO = onlineEditorService.contractTemplateBaseQuery(contractTemplateBaseQueryDTO);
                response = new XbbResponse<>(contractTemplateBaseQueryVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 文件拷贝后保存
     * date: 2023/3/10 10:19
     * @author yingjf
     * @param fileCopySaveDTO
     * @param br
     */
    @RequestMapping(value = "/fileCopy/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String fileCopySave(@RequestBody @Valid FileCopySaveDTO fileCopySaveDTO, BindingResult br) {
        XbbResponse<FileCopySaveVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                FileCopySaveVO fileCopySaveVO = onlineEditorService.fileCopySave(fileCopySaveDTO);
                response = new XbbResponse<>(fileCopySaveVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }


    @RequestMapping(value = "/file/rename", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String fileRename(@RequestBody @Valid FileRenameDTO fileRenameDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                onlineEditorService.fileRename(fileRenameDTO);
                response = new XbbResponse<>(new BaseVO());
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/file/detail", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String fileDetail(@RequestBody @Valid FileDetailDTO fileDetailDTO, BindingResult br) {
        XbbResponse<FileDetailVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                FileDetailVO fileDetailVO = onlineEditorService.fileDetail(fileDetailDTO);
                response = new XbbResponse<>(fileDetailVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

}
